<?php

/*
 * litchicms
 * Copyright (C) 2010-5-1 Author: tiefa(539247@qq.com)
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
!defined('BRK') && exit("!");

/*
 * template_parse_assign 赋值函数
 * @param $string 
 * @return $string
 */

function template_parse_assign($string) {
    //($data="asd")
    //. \\ + * ? [ ^ ] $ ( ) { } = ! < > | :
    $all_base = array();
    preg_match_all("/\((\\\$[^\d][\w\[\]'\"]*?)\s*?\=\s*?([\\\$'\"]+?.*?)\)/is", $string, $all_base);
    /*
     * array(3) {
      [0] => array(3) {
      [0] => string(48) "($CURR['content']=$CURR['content']|md5|md5|sha1)"
      }
      [1] => array(3) {
      [0] => string(16) "$CURR['content']"
      }
      [2] => array(3) {
      [0] => string(29) "$CURR['content']|md5|md5|sha1"
      }
      }


     */
    foreach ($all_base[2] as $k => &$v) {
        $v = "<?php " . $all_base[1][$k] . "=" . template_var_call_fun($v) . " ;?>";
        $all_base[0][$k] = "/" . preg_quote($all_base[0][$k], '/') . "/";
    }

    unset($v);

    $string = preg_replace($all_base[0], $all_base[2], $string, 1);

    //简单赋值END , 下面的是需要使用函数的情况
//    ($data|COLL:id=39:name='asdasd')
    $all_base = array();
    preg_match_all("/\((\\\$[^\d][\w\[\]'\"]*?)\s*?\|\s*?([^\d][\w]*)[:]?(.*?)\)/is", $string, $all_base);

//    dump($all_base);
    /*
     *   [0] => array(1) {
      [0] => string(21) "($data|COLL:39,'ljl')"
      }
      [1] => array(1) {
      [0] => string(5) "$data"
      }
      [2] => array(1) {
      [0] => string(4) "COLL"
      }
      [3] => array(1) {
      [0] => string(8) "39,'ljl'|md5"
      }

     */

    foreach ($all_base[2] as $k => &$v) {


        $all_base[3][$k] = str_replace("\|", "__XIEXIAN__", $all_base[3][$k]);
        $all_base[3][$k] = str_replace("\{", "__LEFT_S__", $all_base[3][$k]);
        $all_base[3][$k] = str_replace("\}", "__RIGHT_S__", $all_base[3][$k]);

        $all_base[3][$k] = str_replace("{", "array(", $all_base[3][$k]);
        $all_base[3][$k] = str_replace("}", ")", $all_base[3][$k]);
        if (($temp = strpos($all_base[3][$k], '|')) === false) {
            $temp2 = "";
        } else {
            $temp2 = substr($all_base[3][$k], $temp);
            $all_base[3][$k] = substr($all_base[3][$k], 0, $temp);
        }

        $v = "$v(" . $all_base[3][$k] . ")$temp2";
        $v = "<?php " . $all_base[1][$k] . "=" . template_var_call_fun($v) . " ;?>";

        $v = str_replace("__XIEXIAN__", "|", $v);
        $v = str_replace("__LEFT_S__", "{", $v);
        $v = str_replace("__RIGHT_S__", "}", $v);

        $all_base[0][$k] = "/" . preg_quote($all_base[0][$k], '/') . "/";
    }

    unset($v);

    $string = preg_replace($all_base[0], $all_base[2], $string, 1);

    return $string;
}
?>